package com.future.wxapp.pedometer.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.future.wxapp.common.util.CalendarUtil;
import com.future.wxapp.pedometer.dao.*;
import com.future.wxapp.pedometer.domain.Type;
import com.future.wxapp.pedometer.service.ShowDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class ShowDataServiceImpl implements ShowDataService {
    @Autowired
    private TypeDao typeDao;
    @Autowired
    private SellDao sellDao;
    @Autowired
    private ChickenDao chickenDao;
    @Autowired
    private StepNumberDao stepNumberDao;
    @Autowired
    private BatchesDao batchesDao;

    @Override
    public List<JSONObject> countByType() {
        return typeDao.getNumberByType();
    }

    @Override
    public Double annualSales() {
        Calendar calendar = Calendar.getInstance();
        CalendarUtil.setMin(1, calendar);
        return sellDao.annualSales(calendar.getTime());
    }

    @Override
    public List<JSONObject> typeSalesVolume() {
        return sellDao.countByType();
    }

    @Override
    public Long totalStep() {
        Calendar begin = Calendar.getInstance();
        CalendarUtil.setMin(2, begin);
        Calendar end = Calendar.getInstance();
        CalendarUtil.setMax(2, end);
        return stepNumberDao.todayTotalStep(begin.getTime(), end.getTime());
    }

    @Override
    public Integer totalChicken() {

        return chickenDao.totalChicken();
    }

    @Override
    public List<JSONObject> stepCountByType() {
        Calendar begin = Calendar.getInstance();
        CalendarUtil.setMin(Calendar.DAY_OF_MONTH, begin);
        Calendar end = Calendar.getInstance();
        CalendarUtil.setMax(Calendar.DAY_OF_MONTH, end);
        return stepNumberDao.todayStepCountByType(begin.getTime(), end.getTime());
    }

    @Override
    public List<JSONObject> salesStatistics() {
        Calendar currentCalendar = Calendar.getInstance();
        String[] quarter = {"一", "二", "三", "四"};
        List<JSONObject> data = new ArrayList<>();
        int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;
        int currentQuarter = currentMonth / 3 + (currentMonth % 3 > 0 ? 1 : 0);

        for (int i = 0; i < currentQuarter; i++) {
            currentCalendar.set(Calendar.MONTH, i * 3);
            CalendarUtil.setMin(Calendar.MONTH, currentCalendar);
            Date quarterStart = currentCalendar.getTime();
            CalendarUtil.setMax(Calendar.MONTH, currentCalendar);
            currentCalendar.set(Calendar.MONTH, i * 3 + 2);
            Date quarterEnd = currentCalendar.getTime();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");
            log.info("季度起始:{},季度结束:{}", dateFormat.format(quarterStart), dateFormat.format(quarterEnd));


            double sales = sellDao.salesStatistics(quarterStart, quarterEnd);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("第" + quarter[i] + "季度", sales);
            data.add(jsonObject);
        }
        return data;
    }

    @Override
    public List<JSONObject> saleVolume() {
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 4; i++) {
            calendar.set(Calendar.MONTH, i + 1);

        }
        return null;
    }

    @Override
    public List<JSONObject> typeSaleStatistics() {
        Calendar currentCalendar = Calendar.getInstance();
        String[] quarters = {"一", "二", "三", "四"};
        int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;
        int currentQuarter = currentMonth / 3 + (currentMonth % 3 > 0 ? 1 : 0);
        /*for (int i = 0; i < 4; i++) {
            currentCalendar.set(Calendar.MONTH, i * 3);
            CalendarUtil.setMin(Calendar.MONTH, currentCalendar);
            Date quarterStart = currentCalendar.getTime();
            CalendarUtil.setMax(Calendar.MONTH, currentCalendar);
            currentCalendar.set(Calendar.MONTH, i * 3 + 2);
            Date quarterEnd = currentCalendar.getTime();

            List<JSONObject> data = chickenDao.typeSaleStatistics(quarterStart, quarterEnd);
            result.add(data);
        }*/
        List<JSONObject> result = new ArrayList<>();

        for (Type type : typeDao.selectAll()) {
            JSONObject data = new JSONObject(true);
            data.put("type", type.getTypeName());
            List<String> quarter = new ArrayList<>();
            List<Double> sellVolume = new ArrayList<>();
            for (int i = 0; i < currentQuarter; i++) {
                quarter.add("第"+quarters[i]+"季");
                currentCalendar.set(Calendar.MONTH, i * 3);
                CalendarUtil.setMin(Calendar.MONTH, currentCalendar);
                Date quarterStart = currentCalendar.getTime();
                CalendarUtil.setMax(Calendar.MONTH, currentCalendar);
                currentCalendar.set(Calendar.MONTH, i * 3 + 2);
                Date quarterEnd = currentCalendar.getTime();
                sellVolume.add(sellDao.getTypeSellVolume(type.getId(), quarterStart, quarterEnd));
            }

            data.put("quarter", quarter);
            data.put("value", sellVolume);
            result.add(data);
        }
        return result;
    }

    @Override
    public List<JSONObject> monthOldSale() {
        return chickenDao.monthOldSale();
    }


}
